//
//  HJSearchView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/16.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

class HJSearchView: UIView {

    @IBOutlet weak var cons_txSearchTrailing: NSLayoutConstraint!
    @IBOutlet weak var tx_search: UITextField!
    @IBOutlet weak var cancleBtn: UIButton!
    class func searchView() ->HJSearchView{
        
        let searchView = NSBundle.mainBundle().loadNibNamed("HJSeatchView", owner: nil, options: nil).last as! HJSearchView
        return searchView

    }
    // MARK: - 设置leftView
    override func awakeFromNib() {
        leftView.frame.size = CGSizeMake(tx_search.frame.height, tx_search.frame.height)
        self.tx_search.leftView = leftView
        //要设置模才能显示
        tx_search.leftViewMode = .Always
        
        //代码方式设置边框
//        tx_search.layer.borderWidth = 1
//        tx_search.layer.borderColor = UIColor.redColor().CGColor
//        tx_search.layer.cornerRadius = 5
//        tx_search.layer.masksToBounds = true
        
        
    }
    // MARK: - 开始编辑
    @IBAction func editingBegan(sender: UITextField) {
        print("-----")
        self.cons_txSearchTrailing.constant = 45
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    //点击取消按钮
    @IBAction func cancleBtnClick(sender: UIButton) {
        cons_txSearchTrailing.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        //取消第一响应者
        self.tx_search.resignFirstResponder()
    }
    
    // MARK: - 懒加载leftView
    lazy var leftView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        imageView.contentMode = .Center
        return imageView
        
    }()
}
